<template>
	<om-page :title="pageTitle">
		<web-view :src="src" @message="videoPostMessage"></web-view>
	</om-page>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				pageTitle:"视频播放",
				src: '',
				videoTypeId: '',
				videoId: '',
				param: '',
			};
		},
		methods: {
			videoPostMessage(data) {
				let _that = this
				let videoDesc = data.detail.data[0]
				let msgType = videoDesc.data.substring(0,1);
				_that.messageHandle(msgType)
			},
			receiveMessage(res) {
				let _that = this;
				let msgType = res.data.substring(0,1);
				_that.messageHandle(msgType)
			},
			messageHandle(msgType){
				let _that = this;
				if(msgType == 1){//记录观看时间
					let saveTime = res.data.substring(2,res.data.length)
					_that.om.request('videoRecord/saveVideoUserInfo', {
						formId: _that.videoId,
						seconds: parseInt(saveTime),
					}, 'get').then(res => {
						
					});
				}
				if(msgType == 3){//播放上一集
					_that.om.request('videoRecord/getLastVideoRecordId', {
						videoRecordId: _that.videoId,
					}, 'get').then(res => {
						if(res.value == 0){
							window.frames[0].postMessage("1:已经是第一集了！", '*');
						}else{
							_that.videoId = res.value
							_that.om.request('videoRecord/updateBrowsedNums', {
								videoRecordId: _that.videoId
							}, 'get').then(res => {
								uni.redirectTo({
									url: '/pages/mine/video/video-webplay?id='+_that.videoId
								});
							})
						}
					});
				}
				if(msgType == 4){//播放下一集
					_that.om.request('videoRecord/getNextVideoRecordId', {
						videoRecordId: _that.videoId,
					}, 'get').then(res => {
						if(res.value == 0){
						    window.frames[0].postMessage("2:已经是最后一集了！", '*');
						}else{
							_that.videoId = res.value
							_that.om.request('videoRecord/updateBrowsedNums', {
								videoRecordId: _that.videoId
							}, 'get').then(res => {
								uni.redirectTo({
									url: '/pages/mine/video/video-webplay?id='+_that.videoId
								});
							})
						}
					});
				}
			},
			getVideo(){
				let _that = this;
				
				let arr = [_that.om.request("videoRecord/getVideo",{
					formId: _that.videoId
				},'post'), _that.om.request("videoRecord/getValidNum"),
				 _that.om.request("videoRecord/getCheckNum")]
				//多个AJAX全部完成时开始执行
				Promise.all(arr).then(res => {
					let obj = res[0].value;
					let browseFlag = obj.browseFlag ? 1 : 5;
					let videoSeconds = obj.videoSeconds;
					let videoBrowseSeconds = obj.videoBrowseSeconds ? obj.videoBrowseSeconds : 0;
					let url = obj.uploadFileData.filePath.substring(11);
					let videoUrl = getApp().globalData.url+url.replace('\\','/')+'/'+obj.uploadFileData.fileSaveName;
					// let videoUrl = 'http://192.168.6.61/'+url.replace('\\','/')+'/'+obj.uploadFileData.fileSaveName;
					let videoName = obj.videoType+'-第'+obj.orderNumber+'课  '+obj.videoName;
					let validNum = res[1].value;
					let checkNum = res[2].value;
					_that.videoTypeId = obj.videoTypeId
					_that.param = '&videoUrl='+videoUrl+'&videoName='+encodeURI(videoName)+'&browseFlag='+browseFlag+
					'&videoSeconds='+videoSeconds+'&videoBrowseSeconds='+videoBrowseSeconds+'&validNum='+validNum+'&checkNum='+checkNum;
					_that.src = '/hybrid/html/webview-video.html?id='+_that.videoId+_that.param;
				})
			},
		},
		onLoad(option) {
			let _that = this
			_that.videoId = option.id;
			_that.getVideo();
			// #ifdef H5
			window.addEventListener("message", _that.receiveMessage, false);
			//#endif
		},
	};
</script>

<style lang="scss" scoped>
	
</style>                                            